package com.yb.guigu.pick.security.handler;

import com.alibaba.fastjson2.JSON;
import com.yb.guigu.pick.cache.manager.api.GuiguPickStringCacheManager;
import com.yb.guigu.pick.security.bean.GuiguAbstractAuthenticationToken;
import com.yb.guigu.pick.security.bean.GuiguPickUser;
import com.yb.guigu.pick.security.bean.LoginSuccessResult;
import com.yb.guigu.pick.security.generator.AccessTokenGenerator;
import com.yb.guigu.pick.core.bean.GuiguResult;
import com.yb.guigu.pick.core.constant.RedisKeyConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/31 13:40
 * FileName: GuiguPickLoginSuccessHandler
 * Description:
 */
public class GuiguPickLoginSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    AccessTokenGenerator accessTokenGenerator;

    @Autowired
    GuiguPickStringCacheManager guiguPickStringCacheManager;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        GuiguAbstractAuthenticationToken authenticationToken= (GuiguAbstractAuthenticationToken) authentication;
        GuiguPickUser guiguPickUser = authenticationToken.getGuiguPickUser();

        String loginAccessToken = guiguPickStringCacheManager.getString(String.format(RedisKeyConstant.LOGIN_USER_ACCESS_TOKEN_KEY, guiguPickUser.getUsername()));
        guiguPickUser.setLoginTime(LocalDateTime.now());

        if(!StringUtils.hasText(loginAccessToken)){

            loginAccessToken = accessTokenGenerator.generate(guiguPickUser);

            guiguPickUser.setLoginAccessToken(loginAccessToken);
            Map<String,String> map=new HashMap<>();
            map.put(String.format(RedisKeyConstant.LOGIN_USER_ACCESS_TOKEN_KEY,guiguPickUser.getUsername()), loginAccessToken);
            map.put(String.format(RedisKeyConstant.LOGIN_USER_DETAIL_KEY,loginAccessToken), JSON.toJSONString(guiguPickUser));
            guiguPickStringCacheManager.multiSet(map);

        }else{
            Map<String,String> map=new HashMap<>();
            guiguPickUser.setLoginAccessToken(loginAccessToken);
            map.put(String.format(RedisKeyConstant.LOGIN_USER_DETAIL_KEY,loginAccessToken), JSON.toJSONString(guiguPickUser));
            guiguPickStringCacheManager.multiSet(map);
        }

        LoginSuccessResult loginSuccessResult = new LoginSuccessResult();
        BeanUtils.copyProperties(guiguPickUser,loginSuccessResult);
        response.getWriter().write(GuiguResult.success(loginSuccessResult).toJSON());
    }
}
